#include "file.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define FILENAME "catdata.txt"

//从文件中读取数据到链表中
void readCat(LNode* pList){
    FILE* fp = fopen(FILENAME,"r");
    if(NULL == fp) return;
    int a;
    Cat temp;
    while(1){
        a = fread(&temp,1,sizeof(Cat),fp);
        if(a<=0) break;

        tailList(pList,temp);
    }
    fclose(fp);
}

//将数据存入文件中
void saveCat(LNode* pList){
    FILE* fs = fopen(FILENAME,"w");
    if(NULL == fs){
        printf("文件打开失败!\n");
    }
    LNode* ps = pList;
    ps = ps->pNext; //跳过头节点
    while(ps){
        fwrite(&(ps->data),1,sizeof(Cat),fs);
        ps = ps->pNext;
    }
    //printf("文件保存成功!\n");
    fclose(fs);
}